package net.thxjj.mobileServer.websocket;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import net.thxjj.mobileServer.entity.AppRespBean;
import net.thxjj.mobileServer.entity.BaseBean;
import net.thxjj.mobileServer.service.IUserService;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;


@Component
public class SpliterHandler extends HeartBeatBaseChannelInBoundHandler {
    private Logger log = Logger.getLogger(String.valueOf(this.getClass()));

    private static Map<String, SimpleChannelInboundHandler> handlerMap = null;


    //解码映射表
    Map<String, Class> codecMap = null;
    private static volatile SpliterHandler instance = null;

    public static SpliterHandler getInstance() {
        if (instance == null) {
            synchronized (SpliterHandler.class) {
                instance = new SpliterHandler();
                return instance;
            }
        }
        return instance;
    }

    public SpliterHandler(IUserService userService) {
        handlerMap = new HashMap<>();
        this.userService = userService;
        AppLoginHandler appLoginHandler = new  AppLoginHandler();
        appLoginHandler.setUserService(userService);

        handlerMap.put(ActionType.APP_LOGIN.getName(), appLoginHandler);

        handlerMap.put(ActionType.HEARTBEAT.getName(),new UserHeartHandler());

        AppSendHandler appSendHandler = new  AppSendHandler();
        appSendHandler.setUserService(userService);
        handlerMap.put(ActionType.APP_SEND.getName(),appSendHandler);

        codecMap = new HashMap<String, Class>();

        codecMap.put(ActionType.APP_LOGIN.getName(), AppLoginHandler.class);
        codecMap.put(ActionType.HEARTBEAT.getName(), UserHeartHandler.class);
        codecMap.put(ActionType.APP_SEND.getName(), AppSendHandler.class);
    }

    public SpliterHandler() {
        handlerMap = new HashMap<>();
        handlerMap.put(ActionType.APP_LOGIN.getName(), new AppLoginHandler());
        handlerMap.put(ActionType.HEARTBEAT.getName(),new UserHeartHandler());
        handlerMap.put(ActionType.APP_SEND.getName(),new AppSendHandler());

        codecMap = new HashMap<String, Class>();

        codecMap.put(ActionType.APP_LOGIN.getName(), AppLoginHandler.class);
        codecMap.put(ActionType.HEARTBEAT.getName(), UserHeartHandler.class);
        codecMap.put(ActionType.APP_SEND.getName(), AppSendHandler.class);
    }

    @Override
    public void chanelReadR(ChannelHandlerContext ctx, TextWebSocketFrame s) {
        try {
            BaseBean baseBean =  JSONObject.parseObject(s.text(), BaseBean.class);
            if(handlerMap.containsKey(baseBean.getAction())){
                ((HeartBeatBaseChannelInBoundHandler)(handlerMap.get(baseBean.getAction()))).chanelReadR(ctx, s);
            }else{
                AppRespBean appRespBean =  new AppRespBean(baseBean.getMsgId(),ActionType.ERROR_RESP.getName(), ErrCode.ACTION_ERROR.getCode(),ErrCode.ACTION_ERROR.getDescription(),null);
                ctx.writeAndFlush(new TextWebSocketFrame(appRespBean.toString()));
            }
        }catch (JSONException e){
            AppRespBean appRespBean =  new AppRespBean(0, ActionType.ERROR_RESP.getName(), ErrCode.JSON_FORMAT_ERROR.getCode(),ErrCode.JSON_FORMAT_ERROR.getDescription(),null);
            ctx.writeAndFlush(new TextWebSocketFrame(appRespBean.toString()));
            if(!AppSocketMap.hasUser(ctx.channel())){
                ctx.close();
            }

        }
    }


    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame frame) throws Exception {
        super.channelRead0(ctx,frame);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelInactive(ctx);
    }

    /**
     * channelRead
     * @param ctx
     * @param msg
     * @throws Exception
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        super.channelRead(ctx,msg);

    }



}
